package com.projectxxxxxxxxxx.screens;

import java.util.TimerTask;
import java.util.TreeMap;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.RemoteViews;

import com.projectxxxxxxxxxx.Constants;
import com.projectxxxxxxxxxx.R;
import com.projectxxxxxxxxxx.info.Info;
import com.projectxxxxxxxxxx.utils.UiUtils;

public class NetworkScreen extends Screen {

	private final long WIDGET_UPDATE_INTERVAL = 500;

	String wifiReceiveSpeed;
	String wifiTransmitSpeed;
	String gprsReceiveSpeed;
	String gprsTransmitSpeed;

	String wifiReceiveTraffic;
	String wifiTransmitTraffic;
	String gprsReceiveTraffic;
	String gprsTransmitTraffic;

	public static Long startWifiReceiveTraffic = 0L;
	public static Long startWifiTransmitTraffic = 0L;
	public static Long startGprsReceiveTraffic = 0L;
	public static Long startGprsTransmitTraffic = 0L;

	public static boolean startWifiTraffic = true;
	public static boolean startGprsTraffic = true;

    public NetworkScreen(final Context context, final int id) {
		super(context, id, "");

	}

    @Override
    public void updateAllView(final RemoteViews views) {

		LoadEnums loadEnums = new LoadEnums(context, views);
		loadEnums.execute("");

		setOnClickListener(views);

        pingTimer.schedule(new TimerTask() {
            @Override
            public void run() {
				requestRedraw(Constants.ACTION_UPDATE_NETWORK_SCREEN);
            }
        }, 0);
    }

    @Override
    public void updateView(final Intent intent, final String action, final RemoteViews views) {

		LoadEnums loadEnums = new LoadEnums(context, views);
		loadEnums.execute(action);
		
        pingTimer.schedule(new TimerTask() {
            @Override
            public void run() {
				requestRedraw(Constants.ACTION_UPDATE_NETWORK_SCREEN);
            }
        }, WIDGET_UPDATE_INTERVAL);
    }

    public class LoadEnums extends AsyncTask<String, Void, String> {

        final Context context;
        final RemoteViews views;
		String[] args;

        public LoadEnums(final Context context, final RemoteViews views) {
            this.views = views;
            this.context = context;
        }

        @Override
        protected String doInBackground(final String... args) {
            this.args = args;
            
			TreeMap<String, String> map = Info.NETWORK.get();

			wifiReceiveSpeed = map.get(Info.INFO_NETWORK_WIFI_RECEIVE_SPEED);
			wifiTransmitSpeed = map.get(Info.INFO_NETWORK_WIFI_TRANSMIT_SPEED);
			gprsReceiveSpeed = map.get(Info.INFO_NETWORK_GPRS_RECEIVE_SPEED);
			gprsTransmitSpeed = map.get(Info.INFO_NETWORK_GPRS_TRANSMIT_SPEED);


			if (startWifiTraffic
					|| startWifiReceiveTraffic > Long.parseLong(map.get(Info.INFO_NETWORK_WIFI_RECEIVE_TRAFFIC))
					|| startWifiTransmitTraffic > Long.parseLong(map.get(Info.INFO_NETWORK_WIFI_TRANSMIT_TRAFFIC))) {
				startWifiReceiveTraffic = Long.parseLong(map.get(Info.INFO_NETWORK_WIFI_RECEIVE_TRAFFIC));
				startWifiTransmitTraffic = Long.parseLong(map.get(Info.INFO_NETWORK_WIFI_TRANSMIT_TRAFFIC));
				wifiReceiveTraffic = UiUtils.humanReadableByteCount(0L, true);
				wifiTransmitTraffic = UiUtils.humanReadableByteCount(0L, true);
				startWifiTraffic = false;
			} else {
				wifiReceiveTraffic = UiUtils.humanReadableByteCount(
						(Long.parseLong(map.get(Info.INFO_NETWORK_WIFI_RECEIVE_TRAFFIC)) - startWifiReceiveTraffic),
						true);
				wifiTransmitTraffic = UiUtils.humanReadableByteCount(
						(Long.parseLong(map.get(Info.INFO_NETWORK_WIFI_TRANSMIT_TRAFFIC)) - startWifiTransmitTraffic),
						true);
			}

			if (startGprsTraffic
					|| startGprsReceiveTraffic > Long.parseLong(map.get(Info.INFO_NETWORK_GPRS_RECEIVE_TRAFFIC))
					|| startGprsTransmitTraffic > Long.parseLong(map.get(Info.INFO_NETWORK_GPRS_TRANSMIT_TRAFFIC))) {
				startGprsReceiveTraffic = Long.parseLong(map.get(Info.INFO_NETWORK_GPRS_RECEIVE_TRAFFIC));
				startGprsTransmitTraffic = Long.parseLong(map.get(Info.INFO_NETWORK_GPRS_TRANSMIT_TRAFFIC));
				gprsReceiveTraffic = UiUtils.humanReadableByteCount(0L, true);
				gprsTransmitTraffic = UiUtils.humanReadableByteCount(0L, true);
				startGprsTraffic = false;
			} else {
				gprsReceiveTraffic = UiUtils.humanReadableByteCount(
						(Long.parseLong(map.get(Info.INFO_NETWORK_GPRS_RECEIVE_TRAFFIC)) - startGprsReceiveTraffic),
						true);
				gprsTransmitTraffic = UiUtils.humanReadableByteCount(
						(Long.parseLong(map.get(Info.INFO_NETWORK_GPRS_TRANSMIT_TRAFFIC)) - startGprsTransmitTraffic),
						true);
			}
            return null;
        }

        @Override
		protected void onPostExecute(final String result) {

			// Log.e(getClass().toString(), "---------------------");
			// Log.e(getClass().toString(), "" + wifiReceiveSpeed);
			// Log.e(getClass().toString(), "" + wifiTransmitSpeed);
			// Log.e(getClass().toString(), "" + wifiReceiveTraffic);
			// Log.e(getClass().toString(), "" + wifiTransmitTraffic);

			views.setTextViewText(R.id.wifi_receive_speed, wifiReceiveSpeed);
			views.setTextViewText(R.id.wifi_transmit_speed, wifiTransmitSpeed);
			views.setTextViewText(R.id.gprs_receive_speed, gprsReceiveSpeed);
			views.setTextViewText(R.id.gprs_transmit_speed, gprsTransmitSpeed);

			views.setTextViewText(R.id.wifi_receive_traffic, wifiReceiveTraffic);
			views.setTextViewText(R.id.wifi_transmit_traffic, wifiTransmitTraffic);
			views.setTextViewText(R.id.gprs_receive_traffic, gprsReceiveTraffic);
			views.setTextViewText(R.id.gprs_transmit_traffic, gprsTransmitTraffic);


			updateWidget(views);

        }

    }

	private void setOnClickListener(final RemoteViews views) {
		Intent wifiIntent = new Intent(Constants.ACTION_NETWORK_SCREEN_WIFI_TRAFIC_CLEAR_CLICK);
		Intent gprsIntent = new Intent(Constants.ACTION_NETWORK_SCREEN_GPRS_TRAFIC_CLEAR_CLICK);

		PendingIntent wifiPendingIntent = PendingIntent.getBroadcast(context, 0, wifiIntent, 0);
		PendingIntent gprsPendingIntent = PendingIntent.getBroadcast(context, 0, gprsIntent, 0);

		views.setOnClickPendingIntent(R.id.wifi_traffic_clear, wifiPendingIntent);
		views.setOnClickPendingIntent(R.id.gprs_traffic_clear, gprsPendingIntent);
	}
}
